# coding: utf-8
import random

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage


def xpath(path):
    return '/home/sp/Projects/cr-dev' + path

def paginate(collection, **kwargs):
    pgn = Paginator(collection, kwargs.get('pp', 15))
    try:
        page = pgn.page(kwargs.get('page', 1))
    except PageNotAnInteger:
        page = pgn.page(kwargs.get('page', 1))
    except EmptyPage:
        page = pgn.page(pgn.num_pages - 1)

    return {'o': pgn, 'p': page}

def captcha_image(text):
    return text

def key_gen(chars=xrange(10), len=10):
    return ''.join([str(random.choice(chars)) for x in xrange(len)])

def captcha_check(request):
    return request.session.get('captcha', key_gen()) == request.POST.get('captcha')
